package com.wangdong.network.utils;

import android.annotation.SuppressLint;
import android.util.Log;

import com.wangdong.network.http.ConfigConstants;
import com.wangdong.network.http.MD5;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@SuppressLint("all")
@SuppressWarnings("all")
public class SignUtils {

	/**
	 * @param args
	 */

//    final static private String SECRET = "d638d14285973d20b2f854ec9dcef7bf";

    public static String join(List<String> r, String d)
    {

        if (r.size() == 0) return "";
        StringBuilder sb = new StringBuilder();
        int i;
        for(i=0;i<r.size()-1;i++)
            sb.append(r.get(i)+d);
        return sb.toString()+r.get(i);
    }
	
	public static String sign(Map<String, String> parameter){
		 Object[]   keys   =     parameter.keySet().toArray();
	        Arrays.sort(keys);

	        ArrayList<String> signArr = new ArrayList<String>();

	        for (int i = 0; i < keys.length; i++) {
	            String key = (String) keys[i];
	            String p =  key+"="+parameter.get(key);
	            signArr.add(p);
	        }

	        String signStr = join(signArr,"")+ ConfigConstants.SOURCE_SECRET;
		Log.d("wangdong",signStr);
		String retValue = "";

	        try {
	        	retValue=new MD5().getMD5(signStr.getBytes("UTF-8")).toLowerCase();
	        	retValue=str2HexStr(retValue);
	            retValue = SHA1(retValue);
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	        }
        return retValue;
	}
	 public static String SHA1(String decript) {
	        try {
	        	MessageDigest digest = MessageDigest
	                    .getInstance("SHA-1");
	            digest.update(decript.getBytes());
	            byte messageDigest[] = digest.digest();
	            // Create Hex String
	            StringBuffer hexString = new StringBuffer();
	            // 字节数组转换为 十六进制 数
	            for (int i = 0; i < messageDigest.length; i++) {
	                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
	                if (shaHex.length() < 2) {
	                    hexString.append(0);
	                }
	                hexString.append(shaHex);
	            }
	            return hexString.toString();
	 
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return "";
	 }
	public static String str2HexStr(String str)
	{      
	  
	    char[] chars = "0123456789ABCDEF".toCharArray();      
	    StringBuilder sb = new StringBuilder("");
	    byte[] bs = str.getBytes();      
	    int bit;      
	        
	    for (int i = 0; i < bs.length; i++)    
	    {      
	        bit = (bs[i] & 0x0f0) >> 4;      
	        sb.append(chars[bit]);      
	        bit = bs[i] & 0x0f;      
	        sb.append(chars[bit]);    
	    }      
	    return sb.toString().trim();      
	}    
	 public static String toHexString(String s)
	 {
	 String str="";
	 for (int i=0;i<s.length();i++)
	 {
	 int ch = (int)s.charAt(i);
	 String s4 = Integer.toHexString(ch);
	 str = str + s4;
	 }
	 return str;
	 } 

    public static JSONArray concatArray(JSONArray... arrs)
            throws JSONException {
        JSONArray result = new JSONArray();
        for (JSONArray arr : arrs) {
            for (int i = 0; i < arr.length(); i++) {
                result.put(arr.get(i));
            }
        }
        return result;
    }

    public static JSONArray concatArrayUseRoot(JSONArray... arrs)
            throws JSONException {



        JSONArray result = arrs[0];

        for (int i = 1; i <arrs.length ; i++) {
            JSONArray r = arrs[i];
            for (int k = 0; k < r.length(); k++) {
                result.put(r.get(i));
            }
        }
        return result;
    }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}
	public static String md5To32(String text) {
		return md5To32(text, "UTF-8");
	}
	public static String md5To32(String text, String charset) {
		if(text == null || "".equals(text)) {
			return null;
		}
		MessageDigest msgDigest = null;
		try {
			msgDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		try {
			msgDigest.update(text.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] bytes = msgDigest.digest();
		return new String(encodeHex(bytes));
	}
	public static char[] encodeHex(byte[] data) {
		int l = data.length;
		char[] out = new char[l << 1];
		// two characters form the hex value.
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
			out[j++] = DIGITS[0x0F & data[i]];
		}
		return out;
	}
	private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6',
		'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
